package com.caishi.lkx.course.service.impl;

import com.caishi.lkx.course.dto.VideoSyncDto;
import com.caishi.lkx.course.mapper.VideoChapterReMapper;
import com.caishi.lkx.course.mapper.VideoInfoMapper;
import com.caishi.lkx.course.model.VideoChapterModel;
import com.caishi.lkx.course.model.VideoChapterReModel;
import com.caishi.lkx.course.model.VideoInfoModel;
import com.caishi.lkx.course.service.IVideoChapterService;
import com.caishi.lkx.course.service.IVideoInfoService;
import com.zzw.common.Wrappers;
import com.zzw.common.entity.IBaseMapper;
import com.zzw.common.utils.UUIDUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;


@Service
public class VideoInfoServiceImpl implements IVideoInfoService {

    @Resource
    private VideoInfoMapper videoInfoMapper;

    @Resource
    private VideoChapterReMapper videoChapterReMapper;

    @Resource
    private IVideoChapterService videoChapterService;

    @Override
    public IBaseMapper<VideoInfoModel, String> getMapper() {
        return videoInfoMapper;
    }



    public boolean insertBatch(List<VideoInfoModel> entityList,String chapterId) {
        List<VideoInfoModel> addEntityList = new ArrayList<>();
        entityList.forEach(e -> {
            List<VideoInfoModel> existVideoInfoList = getMapper().selectList(Wrappers.<VideoInfoModel>lambdaQuery().eq(VideoInfoModel::getAliyunVideoId,e.getAliyunVideoId()).eq(VideoInfoModel::getChapterId,chapterId),1);
            VideoInfoModel existVideoInfo = !existVideoInfoList.isEmpty() ?existVideoInfoList.get(0):null;
            var isNewRel = true;
            if(existVideoInfo != null ){
                getMapper().update(e,Wrappers.<VideoInfoModel>lambdaUpdate().eq(VideoInfoModel::getAliyunVideoId,e.getAliyunVideoId()).eq(VideoInfoModel::getChapterId,chapterId));
                if(videoChapterReMapper.selectCount(Wrappers.<VideoChapterReModel>lambdaQuery().eq(VideoChapterReModel::getVideoId,existVideoInfo.getId()).eq(VideoChapterReModel::getChapterId,chapterId)) > 0){
                    isNewRel = false;
                };
            }else{
                e.setId(UUIDUtil.generateUUIDByTimestamp());
                e.setChapterId(chapterId);
                addEntityList.add(e);
            }
            //增加视频章节关联关系
            if(isNewRel){
                videoChapterReMapper.insert(VideoChapterReModel.builder().chapterId(chapterId).videoId(e.getId()).build());
            }
        });
        if(!addEntityList.isEmpty()){
            return IVideoInfoService.super.insertBatch(addEntityList);
        }
       return true;
    }

    @Override
    @Transactional
    public void insertBatch(VideoSyncDto videoSyncDto) {
        AtomicInteger index = new AtomicInteger();
        List<VideoInfoModel> videoInfoList = videoSyncDto.getVideoList().stream().map(video -> VideoInfoModel.builder().name(video.getTitle()).serverName(video.getTitle()).aliyunVideoId(video.getVideoId()).duration((long) (video.getDuration() * 1000)).categoryId(videoSyncDto.getCategoryId()).coverImg(video.getCoverURL()).sort(index.getAndIncrement()+1).build()).toList();
        insertBatch(videoInfoList,videoSyncDto.getChapterId());
        VideoChapterModel chapter = videoChapterService.getById(videoSyncDto.getChapterId());

        var videoIdList = videoInfoList.stream().map(VideoInfoModel::getId).toList();

        if(chapter.getVideoIds()!= null && !chapter.getVideoIds().isEmpty()){
            List<String> existVideoIds = chapter.getVideoIds().stream().filter(videoIdList::contains).toList();
            if(!existVideoIds.isEmpty()){
                 chapter.getVideoIds().addAll(videoIdList.stream().filter(id->!existVideoIds.contains(id)).toList()) ;
            }else{
                chapter.getVideoIds().addAll(videoIdList);
            }
        }else{
            chapter.setVideoIds(videoIdList);
        }
        chapter.setVideoNum(chapter.getVideoIds().size());
        videoChapterService.update(chapter);
        //同步数量
        videoChapterService.changeVideoCount(videoSyncDto.getChapterId());
    }
}
